package com.yiip.flume.interceptor;

import org.apache.flume.Context;
import org.apache.flume.Event;
import org.apache.flume.interceptor.Interceptor;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class LogETLInterceptor implements Interceptor {
    @Override
    public void initialize() {

    }

    @Override
    public Event intercept(Event event) {
        //1、取到日志内容
        byte[] body = event.getBody();
        String content = new String(body,Charset.forName("UTF-8"));

        //2、校验一下content。是否符合启动日志，和事件日志两种。
        if(LogsUtil.validateStartLog(content)){
            return event;
        }
        if(LogsUtil.valiteEventLog(content)){
            return event;
        }

        return null;
    }

    @Override
    public List<Event> intercept(List<Event> events) {
        List list = new ArrayList();
        for (Event event : events) {
            Event eventItem = intercept(event);
            if(eventItem != null){
                list.add(eventItem);
            }
        }
        return list;
    }

    @Override
    public void close() {

    }

    public static class Builder implements Interceptor.Builder{

        @Override
        public Interceptor build() {
            return new LogETLInterceptor();
        }

        @Override
        public void configure(Context context) {

        }
    }
}
